﻿using AutoMapper;
using MachineVision.Domain;
using MachineVision.Service.Dto;

namespace MachineVision.Service
{
    public class MissionService : IMissionService
    {
        private readonly VisionDbContext context;
        private readonly IMapper mapper;

        public MissionService(VisionDbContext context, IMapper mapper)
        {
            this.mapper = mapper;
            this.context = context;
        }

        public MissionHistory GetMission(int Id)
        {
            var data =  context.MissionHistoryInfos.FirstOrDefault(s => s.Id.Equals(Id));
            return mapper.Map<MissionHistory>(data);
        }

        public MissionNum GetMissionNum(string num)
        {
            var data = context.MissionWorkNums.FirstOrDefault(s => s.WorkNum.Equals(num));
            return mapper.Map<MissionNum>(data);
        }

        public List<MissionHistory> GetMissions(out int total,int pageNum = 1, int pageSize = 10, DateTime? TimeBegin = null, DateTime? TimeEnd = null)
        {

            IEnumerable<MissionHistoryInfo> data = context.MissionHistoryInfos;
            total = data.Count();

            if (TimeBegin != null)
                data = data.Where(s => s.StartTime >= TimeBegin);
            if (TimeEnd != null)
                data = data.Where(s => s.EndTime <= TimeEnd);

            data = data.OrderByDescending(s => s.CreateTime);
            var list= data.Skip((pageNum-1) * pageSize).Take(pageSize).ToList();
            return mapper.Map<List<MissionHistory>>(list);
        }

        public bool AddMission(MissionHistory value)
        {
            var now = DateTime.Now;
            if (!string.IsNullOrEmpty(value.WorkNum) && !context.MissionWorkNums.Any(s=>s.WorkNum.Equals(value.WorkNum)))
            {
                context.MissionWorkNums.Add(new MissionWorkNum
                {
                    StartTime = value.StartTime,
                    CreateTime = now,
                    WorkNum = value.WorkNum,
                    Name = now.ToString("yy-MM-dd HH:mm")+"巡检",
                });
            }
            MissionHistoryInfo data = mapper.Map<MissionHistoryInfo>(value);
            data.CreateTime = now;
            context.MissionHistoryInfos.Add(data);
            
            int a = context.SaveChanges();
            return a>=1 ;
        }


        public List<MissionNum> GetMissionNums(out int total, int pageNum = 1, int pageSize = 10, DateTime? TimeBegin = null, DateTime? TimeEnd = null)
        {

            IEnumerable<MissionWorkNum> data = context.MissionWorkNums;
            total = data.Count();

            if (TimeBegin != null)
                data = data.Where(s => s.StartTime >= TimeBegin);
            if (TimeEnd != null)
                data = data.Where(s => s.EndTime <= TimeEnd);

            data = data.OrderByDescending(s => s.CreateTime);
            var list = data.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
            return mapper.Map<List<MissionNum>>(list);
        }
        public List<MissionHistory> Get1Missions(string MissionNum, out int total, int pageNum = 1, int pageSize = 10)
        {
            IEnumerable<MissionHistoryInfo> data = context.MissionHistoryInfos.Where(s=>s.WorkNum == MissionNum).OrderByDescending(s => s.CreateTime);
            total = data.Count();

            var list = data.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
            return mapper.Map<List<MissionHistory>>(list);
        }


        /// <summary>
        /// 报警
        /// </summary>
        /// <returns></returns>


        public Warning GetWarning(int Id)
        {
            var data = context.WarningInfos.FirstOrDefault(s => s.Id.Equals(Id));
            return mapper.Map<Warning>(data);
        }

        public List<Warning> GetWarnings(out int total, int pageNum = 1, int pageSize = 10, DateTime? TimeBegin = null, DateTime? TimeEnd = null)
        {
            IEnumerable<WarningInfo> data = context.WarningInfos;
            total = data.Count();

            if (TimeBegin != null)
                data = data.Where(s => s.Time >= TimeBegin);
            if (TimeEnd != null)
                data = data.Where(s => s.Time <= TimeEnd);

            data = data.OrderByDescending(s => s.CreateTime)
                .Skip((pageNum - 1 )* pageSize).Take(pageSize);

            return mapper.Map<List<Warning>>(data);
        }

        public bool AddWarning(Warning value)
        {
            WarningInfo data = mapper.Map<WarningInfo>(value);
            data.CreateTime = DateTime.Now;
            context.WarningInfos.Add(data);
            int a = context.SaveChanges();
            return a >= 1;
        }

    }
}